#include <dirent.h>
#include "include/ucwebimagejpeg2webp.h"
#include "include/ucwebimagejpeg2jpeg.h"

#define JPEG2JPEG(infile,quality,percentage_resize,count) \
{ ucwebImageJpeg2Jpeg image(infile, quality, percentage_resize, count); \
  image.CompressByJpeglib(); \
}

#define JPEG2WEBP(infile, quality, percentage_resize, count) \
{ ucwebImageJpeg2Webp image(infile, quality, percentage_resize, count); \
  if (-1 == image.DecomJpegResizeWebp()) \
    continue; \
}
  //ucimage.DecomJpegResizeGm();
  //ucimage.DecomJpegResizeWebp();
  //ucimage.DecomGmResizeWebp(); 
struct FileList {
  char filename[500];
  struct FileList *next;
};

int jpeg2webp(struct FileList* node, int quality, float percentage_resize);
int jpeg2jpeg(struct FileList* node, int quality, float percentage_resize);
void readDirectory(struct FileList *start, char* dir);

int main(int argc, char **argv)
{
  InitializeMagick(*argv);
  if (argc < 5) {
    fprintf(stderr, "Error! Please input righth agrments for the program!\n");
    return -1;
  }
 
  int quality = atoi(argv[2]);
  float percentage_resize = atof(argv[3]);
  int times = atoi(argv[4]);
 
  cout << "quality = " << quality << endl
      << "percentage_resize = " << percentage_resize << endl
      << "filename = " << argv[1] << endl
      << "times = " << times << endl;
  /*
  //Testing for a picture!
  ucwebImageJpeg2Jpeg image(argv[1], quality, percentage_resize, argv[1]); 
  image.DecomJpegResizeJpegComJpeg();
  //image.DecomGmResizeGmComGm();
  */
  struct FileList start, *node;
  readDirectory(&start, argv[1]);
  node = start.next;
  
  //jpeg2webp(node, quality, percentage_resize);   // jpeg to webp.
  jpeg2jpeg(node, quality, percentage_resize);    // jpeg to jpeg.

  while(start.next) {
    node = start.next;
    start.next = node->next;
    free(node);
    node = NULL;
  }
 
  return 0;
}


int jpeg2webp(struct FileList* node, int quality, float percentage_resize)
{
  /*timeval startt, end;
  gettimeofday(&startt, NULL);
  */
  
  time_t startt, end;
  startt = clock();
  int count = 0;
  while (node) {
    ucwebImageJpeg2Webp image(node->filename, quality, percentage_resize, node->filename);
    if (-1 == image.DecomJpegResizeGm()) {
      printf("%s\n", node->filename);  
      node = node->next;
      continue;
    }
    node = node->next;
     count++;
  }
  cout << count << endl;
  //gettimeofday(&end, NULL);
  //unsigned int timeuse = 1000000 * (end.tv_sec - startt.tv_sec) + (end.tv_usec - startt.tv_usec);
  end = clock();
  cout << "timeuse = " << /*timeuse*/ double(end - startt)/CLOCKS_PER_SEC << endl;
  
  /*  
  timeval start, end;
  gettimeofday(&start, NULL);
  do {
    JPEG2WEBP(argv[1], quality, percentage_resize, argv[1]);
    //JPEG2JPEG(argv[1], quality, percentage_resize, count);
    count++;
  }while(count < times);

  gettimeofday(&end, NULL);
  unsigned int timeuse = 1000000 * (end.tv_sec - start.tv_sec) + (end.tv_usec - start.tv_usec);
  cout << "timeuse = " << timeuse << endl;
  */
}


int jpeg2jpeg(struct FileList* node, int quality, float percentage_resize)
{
  time_t startt, end;
  startt = clock();
  int count = 0;
  while (node) {
    ucwebImageJpeg2Jpeg image(node->filename, quality, percentage_resize, node->filename);
    //DecomJpegResizeJpegComJpeg()   
    //
    if (-1 == image.DecomJpegResizeJpegComJpeg()) {
      printf("%s\n", node->filename);  
      node = node->next;
      continue;
    }
    node = node->next;
     count++;
  }
  cout << count << endl;
  end = clock();
  cout << "timeuse = " << /*timeuse*/ double(end - startt)/CLOCKS_PER_SEC << endl;
}


void readDirectory(struct FileList *start, char* dir)
{
  DIR* directory;
  struct dirent* entry;
  struct FileList *node;
  
  if ((directory=opendir(dir))==NULL) {
      printf("Error opening %s\n", dir);
      return ;
  }
  start->next = NULL;
  node = start;
  
  while((entry = readdir(directory)) != NULL) {
    if ((entry->d_name[0] == '.') || strcmp(entry->d_name, "..") == 0 || strcmp(entry->d_name, "out") == 0 || strcmp(entry->d_name, "delegates.mgk") == 0 || strcmp(entry->d_name, "magic.mgk") == 0) {
      continue;
    }
    node->next = (struct FileList*)malloc(sizeof(struct FileList));
    node = node->next;
    strcpy(node->filename, entry->d_name);
    node->next = NULL;
  }
  closedir(directory);
}
